package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ArticleClassDTO;
import com.api.shopapi.entity.dto.TrainClassDTO;
import com.api.shopapi.service.ArticleClassService;
import com.api.shopapi.service.StaffService;
import com.api.shopapi.service.TrainClassService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * 培训分类
 * */

@RestController
@RequestMapping("TrainClass")
public class TrainClassController extends BaseController{


    @Autowired
    @Qualifier("TrainClassServiceImplOne")
    private TrainClassService trainClassService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    //添加
    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            String parentTrainClassCode = jsonObject.getString("parentTrainClassCode");
            String icon = jsonObject.getString("icon");
            int sort = jsonObject.getIntValue("sort");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ArticleClass/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            TrainClass trainClass=new TrainClass();
            trainClass.setTrainClassCode(UUID.randomUUID().toString());

            if (parentTrainClassCode!=null&&!parentTrainClassCode.equals("")){
                trainClass.setParentTrainClassCode(parentTrainClassCode);
            }else {
                trainClass.setParentTrainClassCode(emptyUUID);
            }
            trainClass.setName(name);
            trainClass.setIcon(icon);
            trainClass.setSort(sort);
            trainClass.setStatus(1);
            trainClass.setGovernmentCode(staff.getGovernmentCode());
            trainClassService.insert(trainClass);

            Object data = new Object[] { };
            return success(data);

        }catch (Exception e){
            insertError("/ArticleClass/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //修改
    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String trainClassCode = jsonObject.getString("trainClassCode");
            String parentTrainClassCode = jsonObject.getString("parentTrainClassCode");
            String name = jsonObject.getString("name");
            String icon = jsonObject.getString("icon");
            int sort = jsonObject.getIntValue("sort");
            int status = jsonObject.getIntValue("status");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ArticleClass/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            TrainClass trainClass = trainClassService.getByTrainClassCode(trainClassCode);
            trainClass.setName(name);
            trainClass.setIcon(icon);
            trainClass.setSort(sort);
            trainClass.setStatus(status);
            trainClass.setParentTrainClassCode(parentTrainClassCode);
            trainClassService.update(trainClass);

            Object data = new Object[] { };
            return success(data);

        }catch (Exception e){
            e.printStackTrace();
            insertError("/ArticleClass/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //查看社区下所有活动分类列表
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int pageIndex = jsonObject.getIntValue("pageIndex");
            int pageSize = jsonObject.getIntValue("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ArticleClass/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<TrainClassDTO> classDTOPageInfo =
                    trainClassService.getPage(pageIndex, pageSize, name, staff.getGovernmentCode());

            return success(classDTOPageInfo);

        }catch (Exception e){
            insertError("/ArticleClass/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "List",method = RequestMethod.POST)
    public MyResult List(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ArticleClass/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);
            List<TrainClassDTO> articleClassDTOList = trainClassService.getListByGovernmentCode(staff.getGovernmentCode());

            return success(articleClassDTOList);
        }catch (Exception e){
            insertError("/ArticleClass/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


}
